package opd.db.services.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import opd.db.services.AbstractJDBCTemplateDao;
import opd.db.services.ITargetsDao;
import opd.domain.Targets;

public class TargetsDao extends AbstractJDBCTemplateDao implements ITargetsDao {
	
	// -------------------- //
	// -- SQL Statements -- // ----------------------------------------------------------------------------
	// -------------------- //
	
	private final String SQL_GET_LAST_TARGETS = "SELECT TARGET_ID, TARGET_TOTALS, TARGET_UTILIZATION, TARGET_DATE FROM TARGETS WHERE TARGET_ID = (SELECT MAX(TARGET_ID) FROM TARGETS)";
	
	
	// ---------------- //
	// -- Row Mapper -- // ---------------------------------------------------------------------------------
	// ---------------- //
	
	class TargetsRowMapper implements RowMapper<Targets>{

		@Override
		public Targets mapRow(ResultSet rs, int row) throws SQLException {
			Targets targets = new Targets();
            
			targets.setId(rs.getInt("TARGET_ID"));
			targets.setTotals(rs.getInt("TARGET_TOTALS"));
			targets.setUtilization(rs.getInt("TARGET_UTILIZATION"));
			targets.setTargetDate(rs.getDate("TARGET_DATE"));
			
			return targets;
		}
		
	}
	
	// ------------------------------------------------------------------------------------------------------
	
	@Override
	@Transactional
	public Targets getLastTargets() {
		return jdbcTemplate.queryForObject(SQL_GET_LAST_TARGETS, new TargetsRowMapper());
	}

}
